<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>实现一个盒子水平居中</title>
    <style>
      /* 方法1 margin: 0 auto*/
      /* .parent {
        width: 400px;
        height: 200px;
        background-color: aqua;
      }
      .child {
        width: 200px;
        height: 200px;
        margin: 0 auto;
        background-color: chocolate;
      } */

      /* 方法2 定位*/
      /* .parent {
        position: relative;
        width: 400px;
        height: 200px;
        background-color: aqua;
      }
      .child {
        position: absolute;
        width: 200px;
        height: 200px;
        left: 50%;
        transform: translate(-50%);
        background-color: chocolate;
      } */

      /* 方法3  flex布局*/
      .parent {
        display: flex;
        width: 400px;
        height: 200px;
        background-color: aqua;
        justify-content: center;
      }
      .child {
        width: 200px;
        height: 200px;
        background-color: chocolate;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child"></div>
    </div>
  </body>
</html>
